#!/bin/bash -
#===============================================================================
#          FILE : a.sh
#         USAGE : ./a.sh
#        AUTHOR : david
#       CREATED : 2017-10-31 10:36:46
#===============================================================================

baseurl="http://openwrt-dist.sourceforge.net/packages/OpenWrt"

dlist="base/ar71xx luci"

for dir in ${dlist}; do
	pushd ${dir}

	rm -f 1.gz
	echo get Packages.gz from ${baseurl}/${dir}/Packages.gz
	#axel -k -n 16 -a ${baseurl}/${dir}/Packages.gz -o 1.gz
	curl -s ${baseurl}/${dir}/Packages.gz -o 1.gz
	if [ $? -ne 0 ]; then
		echo "error when get file from ${baseurl}/${dir}/Packages.gz"
		exit 1
	fi

	if [ -f Packages.gz ]; then
		diff Packages.gz 1.gz
		if [ $? -eq 0 ]; then
			rm -f 1.gz
			popd
			continue
		fi
		md5sum Packages.gz 1.gz
		ls -l
	fi
	cp -f 1.gz Packages.gz
	gunzip Packages.gz

	rm -f *.ipk
	awk -F":" '($1 == "Filename"){print $2}' Packages | awk -v url="${baseurl}/${dir}" '{print "axel -k -n 16 -a "url"/"$1}' >1.sh
	echo ""====================""
	cat 1.sh
	echo ""====================""
	bash 1.sh
	rm -f 1.sh Packages

	mv -f 1.gz Packages.gz
	popd
done

